//
//  AddCommentVc.m
//  WGFGlobalHouses
//
//  Created by Andy on 15/7/22.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "AddCommentVc.h"
#import "config.h"
#import "HideTextVeiw.h"
#import "Tools.h"
#import "PhootSelecteView.h"
#import "MBProgressHUD+MJ.h"
#import "HourseDataManage.h"
#import "ImgCache.h"
#import "PhotoShowManage.h"





@interface AddCommentVc ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate,MWPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet HideTextVeiw *commentBg;
@property (weak, nonatomic) IBOutlet UILabel *tipLab;
@property (strong, nonatomic) UIButton *addPicBtn;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (nonatomic,strong)PhootSelecteView *menuView;
@property (strong, nonatomic) HourseDataManage *dm;

@property (strong, nonatomic) UIButton *btn1,*btn2,*btn3;

@end

@implementation AddCommentVc
@synthesize addPicBtn,dataArray,menuView,dm,hourseId;





- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}


-(void)initView{
    [super initView];
    
    [self.titleView setMianTitle:@"用户点评"];
    [self.titleView setRightImg:@""];
    [[self.titleView doneBtn]setTag:13];
    [[self.titleView doneBtn]addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [[self.titleView doneBtn]setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.commentBg setHideText:@"对楼盘感觉如何？点评一下吧"];
    self.commentBg.delegate =self;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    addPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addPicBtn setBackgroundColor:UIColorFromRGB(0xf4f4f4)];
    [addPicBtn setImage:[UIImage imageNamed:@"cm"] forState:UIControlStateNormal];
    [addPicBtn setImage:[UIImage imageNamed:@"cm"] forState:UIControlStateHighlighted];
    [addPicBtn setTag:12];
    [addPicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addPicBtn];
    
    
    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn1 setTag:16];
    [self.btn1 addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn2 setTag:17];
    [self.btn2 addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn3 setTag:18];
    [self.btn3 addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    dataArray = [NSMutableArray new];
    dm = [HourseDataManage new];
    
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self layoutView];
}


-(void)btnOnclick:(UIButton*)sender{
    
    if (sender.tag==12) {//照片
        
        if (self.menuView==nil) {
            menuView = [PhootSelecteView creadInstance];
            [menuView.tackPicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
            [menuView.selectePicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (dataArray.count<3) {
            [menuView showInView:self.view];
        }else{
            [MBProgressHUD showError:@"最多只能添加三张照片"];
        }
    }else if (sender==menuView.tackPicBtn) {
        
        
        [menuView hide];
        
        // 跳转到相机
        NSUInteger sourceType = UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
        
    }else if(sender==menuView.selectePicBtn){
        
        [menuView hide];
        // 跳转到相册
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }else if(sender.tag == 16){//btn1
        
        MWPhotoBrowser *br = [PhotoShowManage  getMWPhotoShowInstance:self];
        [br setCurrentPhotoIndex:0];
        br.displayActionButton = true;
        [self.navigationController pushViewController:br animated:YES];
        
    }else if(sender.tag == 17){//btn2
        
        MWPhotoBrowser *br = [PhotoShowManage  getMWPhotoShowInstance:self];
        [br setCurrentPhotoIndex:1];
        br.displayActionButton = true;
        [self.navigationController pushViewController:br animated:YES];
        
    }else if(sender.tag == 18){//btn3
        
        MWPhotoBrowser *br = [PhotoShowManage  getMWPhotoShowInstance:self];
        [br setCurrentPhotoIndex:2];
        br.displayActionButton = true;
        [self.navigationController pushViewController:br animated:YES];
        
    }else if (sender.tag==13){//发布
        
        
        if([self.titleView.doneBtn.titleLabel.text isEqualToString:@"完成"]){
            [self.titleView.doneBtn setTitle:@"发布" forState:UIControlStateNormal];
            
            [self.commentBg resignFirstResponder];
        }else{
        
            [MBProgressHUD showMessage:@"请稍后..."];
            __weak typeof(self)baseSelf = self;
            [dm addComment:self.hourseId withString:self.commentBg.text withImgs:self.dataArray with:^(ResulteMod *resultMod) {
                
                [MBProgressHUD hideHUD];
                
                if (resultMod.resltState==ResultSuccess) {
                    
                    [MBProgressHUD showSuccess:@"评论审核中..."];
                    [baseSelf.navigationController popViewControllerAnimated:YES];
                    
                }else{
                    
                    [MBProgressHUD showError:resultMod.resultTip];
                    
                }
            }];
        }
    }
}



-(void)layoutView{
    
    
    [self.btn1 removeFromSuperview];
    [self.btn2 removeFromSuperview];
    [self.btn3 removeFromSuperview];
    
    
    if (dataArray.count==0) {
        
        [addPicBtn setFrame:CGRectMake(10, GET_VIEW_BOTTOM_POS(self.tipLab)+10, PMW-20, 60)];
    }else{
        
        
        int lastPosX = 0;
        int mid = (PMW-60*3)/4;
        if (dataArray.count>0) {
            
            [self.view addSubview:self.btn1];
            
            [self.btn1 setFrame:CGRectMake(mid, self.addPicBtn.frame.origin.y, 60, 60)];
            [self.btn1 setBackgroundImage:[UIImage imageWithData:[ImgCache objectForKey:[dataArray firstObject]]] forState:UIControlStateNormal];
            [self.view addSubview:self.btn1];
            
            lastPosX = GET_VIEW_RIGHT_POS(self.btn1);
        }
        
        if (dataArray.count>1){
            
            [self.btn2 setFrame:CGRectMake(GET_VIEW_RIGHT_POS(self.btn1)+mid, self.addPicBtn.frame.origin.y, 60, 60)];
            [self.btn2 setBackgroundImage:[UIImage imageWithData:[ImgCache objectForKey:[dataArray objectAtIndex:1]]] forState:UIControlStateNormal];
            [self.view addSubview:self.btn2];
            
            lastPosX = GET_VIEW_RIGHT_POS(self.btn2);
        }
        
        
        if (dataArray.count>2){
            
            [self.btn3 setFrame:CGRectMake(GET_VIEW_RIGHT_POS(self.btn2)+mid, self.addPicBtn.frame.origin.y, 60, 60)];
            [self.btn3 setBackgroundImage:[UIImage imageWithData:[ImgCache objectForKey:[dataArray objectAtIndex:2]]] forState:UIControlStateNormal];
            [self.view addSubview:self.btn3];
            
            [self.addPicBtn setHidden:YES];
            
            lastPosX = GET_VIEW_RIGHT_POS(self.btn3);
        }
        
        CGRect buf = self.addPicBtn.frame;
        buf.origin.x = lastPosX+mid;
        buf.size.width = 60;
        
        [self.addPicBtn setFrame:buf];
        
        
        if (dataArray.count>2){
            [self.addPicBtn setHidden:YES];
        }else{
            [self.addPicBtn setHidden:false];
        }
    }
}




#pragma mark  UiPickView delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    image = [Tools fixOrientation:image];
    
    int h = [Tools getHightDifPMsW:image.size.width sh:image.size.height nw:PMW];
    
    image = [Tools imageWithImageSimple:image scaledToSize:CGSizeMake(PMW,h)];
    
    NSString *key = [Tools getSingleFileName];
    [ImgCache setObject:UIImageJPEGRepresentation(image, 1.0) forKey:key];
    
    [dataArray addObject:key];
    
    [self layoutView];
}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}


#pragma mark textView delegate
- (void)textViewDidChange:(UITextView *)textView{
    [self.commentBg textDicChange:textView.text];
    
    NSInteger leng = [[textView text]length];
    if (leng<15) {
        [self.tipLab setText:[NSString stringWithFormat:@"您还差%d个字哦，加油！",15-leng]];
    }else{
        [self.tipLab setText:@""];
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

    if ([text isEqualToString:@""]) {
        return true;
    }
    
    NSInteger leng = [[textView text]length];
    if (leng>200) {
        [MBProgressHUD showError:@"不能超过200个字符"];
        return false;
    }
    return true;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    [self.titleView.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    return true;
}




#pragma MWPhotoShowDelegate
#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    
    return dataArray.count;
}


- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    
    
    if (index < dataArray.count){
        
        UIImage *img = [UIImage imageWithData:[ImgCache objectForKey:[dataArray objectAtIndex:index]]];
        
        return  [MWPhoto photoWithImage:img];
    }
        
    return nil;
}

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index{
    
    [dataArray removeObjectAtIndex:index];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



@end
